// page/category/prolist.js
var sliderWidth = 96; // 需要设置slider的宽度，用于计算中间位置
Page({
  data: {
      isHide: 0, 
      tabs: ["", "", "",""],
      activeIndex: 0,
      sliderOffset: 0, 
      showDown: 0,
      num1:[],
      productItems: [],  //显示的
      list: [],  //全部的
      status:0,
      p:0,
      use:'',
      colour: '',
      genre: '',
      num: '',
  },
  onLoad: function (options) {
    var that = this;
    var app = getApp()
    that.setData({
        use: options['use'],
        colour: options['colour'],
        genre: options['genre'],
        num: options['num'],
    })
    wx.request({
      url: app.globalData.host + '&g=App&m=Category&a=prolist',
      data: {
          use: that.data.use,
          colour: that.data.colour,
          genre: that.data.genre, 
          num: that.data.num, 
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        // console.log(res)
        that.setData({
          productItems: res.data.d,
          list: res.data.d,
          num1: res.data.n,
          p: res.data.page
        })
        wx.setNavigationBarTitle({
          title: that.data.navtitle//页面标题为路由参数
        })
      }
    })

    
      wx.getSystemInfo({
          success: function (res) {
              that.setData({
                  isHide: 1,
                  sliderLeft: (res.windowWidth / that.data.tabs.length - sliderWidth)/2 ,
                  sliderOffset: res.windowWidth / that.data.tabs.length * that.data.activeIndex
              });
          }
      });      
  },
  onShow: function (options){
      var that = this;
      var app = getApp()
      that.setData({
          use: options['use'],
          colour: options['colour'],
          genre: options['genre'],
          num: options['num'],
      })
  },
  
  
  tabClick: function (e) {  
      this.setData({
          sliderOffset: e.currentTarget.offsetLeft,
          activeIndex: e.currentTarget.id
      });
      if (e.currentTarget.id==0){
          this.setData({
              showDown: 1
          });
      } else if (e.currentTarget.id == 1){
        this.setData({
          showDown: 2
        });
      }else{
        var that = this;
        var k = e.currentTarget.dataset.sas
        let arr = that.data.list;
        var t = that.data. status
        if (t==0){
          for (var i = 0; i <= arr.length - 1; i++) {
            for (var j = 0; j < arr.length - 1 - i; j++) {
                if (parseFloat(arr[j][k]) > parseFloat(arr[j + 1][k])) {
                var temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
              }
            }
          }
          that.setData({
            productItems: arr,
            status: 1,
            showDown: 0
          })  
        }else{
          for (var i = 0; i <= arr.length - 1; i++) {
            for (var j = 0; j < arr.length - 1 - i; j++) {
              if (arr[j][k] < arr[j + 1][k]) {
                var temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
              }
            }
          }
          that.setData({
            productItems: arr,
            status: 0,
            showDown: 0
          })  
        }
      }
       
  },

  closeDropDown: function(e){//销量 人气 评价
    var that = this;
   var k= e.currentTarget.dataset.sas
    let arr = that.data.list;

    for (var i = 0; i <= arr.length - 1; i++) {
      for (var j = 0; j < arr.length - 1 - i; j++) {
        if (arr[j][k] < arr[j + 1][k]) {
          var temp = arr[j];
          arr[j] = arr[j + 1];
          arr[j + 1] = temp;
        }
      }
    }
    that.setData({
      productItems: arr,
      showDown: 0
    })
  },

  numlist: function (e) {//销量 人气 评价

    var that = this;
    var k = e.currentTarget.dataset.id
    let arr = that.data.list;
    var list = [];
    for (var i = 0; i <= arr.length - 1; i++) {
      if (arr[i]['num'] == k) {
        list.push(arr[i])
        }
    }
    that.setData({
      productItems: list,
      showDown: 0
    })
  }, onReachBottom: function () {
      var that = this;
      var app = getApp()
    
      wx.request({
          url: app.globalData.host + '&g=App&m=Category&a=prolist',
          data: {
              use: that.data.use,
              colour: that.data.colour,
              genre: that.data.genre,
              num: that.data.num,
              p: parseInt(that.data.p)+1
          },
          header: {
              'content-type': 'application/json'
          },
          success: function (res) {
            
              var list = that.data.list
              list=list.concat(res.data.d)
            //   console.log(list)
              that.setData({
                  p: res.data.page,
                  list: list,
                  productItems: list,

              })
          }
      })
  },
})
